package iagovsothello.ai;

import iagovsothello.core.*;
import iagovsothello.logger.Entry;
import iagovsothello.logger.Log;

import java.util.*;

public class Randomizer extends Agent {

	public static final String name = "Randomizer";
	private static final long serialVersionUID = 9027774750571431268L;
	private Random generator;
	
	public Randomizer()
	{
		super();
		this.generator = new Random();
	}
	
	public Randomizer(Random generator)
	{
		super();
		this.generator = generator;
	}
	
	public Randomizer(int playerNumber, Random generator) 
	{
		super(playerNumber);
		this.generator = generator;
	}

	public Randomizer(int playerNumber, boolean debugMode, Random generator) 
	{
		super(playerNumber, debugMode);
		this.generator = generator;
	}

	public Randomizer(int playerNumber, boolean debugMode, Log log, Random generator)
	{
		super(playerNumber, debugMode, log);
		this.generator = generator;
	}

	public String getName()
	{
		return name;
	}
	
	public Randomizer clone()
	{
		Randomizer copy = new Randomizer(new Random());
		copyAgentAttributes(copy);
		return copy;
	}
	
	@Override
	protected Position computeNextMove(GameState currentState)
	{
		Position p = null;
		List<Move> legalMoves = currentState.getLegalMoves();
		if (!legalMoves.isEmpty())
		{
			p = legalMoves.get(generator.nextInt(legalMoves.size())).getPosition();
		}
		if (log != null)
		{
			Entry e = new Entry();
			e.selectedPosition = p;
			e.currentPlayer = currentState.getCurrentPlayer();
			e.maxDepth = 1;
			e.nodes = legalMoves.size();
			e.branchingFactor = legalMoves.size();
			e.timeMicros = 0;
			e.turn = currentState.getTurnNumber();
			log.add(e);
		}
		return p;
	}
	
	public String toString()
	{
		return getName();
	}
}
